import Vue from 'vue'
import Vuex from 'vuex'
import _axios from '../plugins/axios'
Vue.use(Vuex)
    // 使用vuex插件

export default new Vuex.Store({
    state: {
        homepageArray: [], //触底加载数据，整合
        historyArray: [], //搜索历史记录
        searchArray: [], //搜索影片数组
        cinemaArray: [], //电影院数组，下单页面需要数据
        beforArray: '广州', //当前城市
        cityArray: ['广州'], //浏览过的城市
        token: null,
        userInfo: null, //用户信息
        allLikeid: null, //想看ID
    },
    getters: {
        // 获取token
        loginToken(state) {
            if (!state.token) {
                state.token = localStorage.getItem('token')
            }
            return state.token
        },
        // 获取登录用户信息
        userInfo(state) {
            if (!state.userInfo) {
                state.userInfo = JSON.parse(localStorage.getItem('userInfo'))
            }
            return state.userInfo
        },
        //取出id信息
        allLikeid(state) {
            if (!state.allLikeid) {
                state.allLikeid = JSON.parse(localStorage.getItem("allLikeid"));
            }
            return state.allLikeid;
        }

    },
    mutations: {
        // 修改触底加载数据
        changeHomepage(state, payload) {
            state.homepageArray = payload; //修改数据
        },
        // 修改历史记录
        changeHistoryArray(state, value) {
            state.historyArray = value
        },
        // 搜索触底加载
        changeSearchArray(state, value) {
            state.searchArray = value
        },
        // 电影院数组，下单页面需要数据
        changeCinemaArray(state, value) {
            state.cinemaArray = value
        },
        // 访问过的城市
        changeCityArray(state, value) {
            state.cityArray = value
        },
        // 当前城市
        changeNowCityArray(state, value) {
            state.beforArray = value
        },
        // 存储token
        changeLoginToken(state, value) {
            state.token = value;
            // 防止页面刷新
            localStorage.setItem('token', value)
        },
        // 存储用户登录信息
        changeUserInfo(state, value) {
            state.userInfo = value;
            localStorage.setItem('userInfo', JSON.stringify(value))
        },
        // 存储id
        changeAllLike(state, value) {
            localStorage.setItem("allLikeid", JSON.stringify(value))
            state.allLikeid = value;
        },
        // 退出登录
        changeLoginOut(state, value) {
            localStorage.removeItem('userInfo')
            localStorage.removeItem('allLikeid')
            localStorage.removeItem('token')
            state.token = null;
            state.userInfo = null;
            state.allLikeid = null;
        }
    },
    actions: {
        // 异步获取用户信息
        getUserInfo(context) {
            _axios.get("http://120.78.9.102:8006/api/userinfo").then((res) => {
                context.commit('changeUserInfo', res)
            }).catch(err => {
                console.log('err---->', err);
            })
        },
        // 获取想看所有ID
        getLikeId(context) {
            console.log('------ID---->', this.getters.userInfo.id);
            let th_id = this.getters.userInfo.id
            _axios.post("http://120.78.9.102:8006/api/datalike", {
                user_id: th_id,
            }).then(res => {
                console.log('获取所有想看id---vuex', res.result);
                context.commit("changeAllLike", res);
            })
        }
    },
})